package com.compass.mobile.presenters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.leanback.widget.Presenter
import com.compass.mobile.R
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.models.Doctor
import dp2px
import setUrlRound

/**
 * Description：
 * Date:2024/8/20 14:19
 */
class DoctorStaticPresenter (private var itemType: String) : Presenter() {

    override fun onCreateViewHolder(viewGroup: ViewGroup): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.item_doctor_static, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: Presenter.ViewHolder, item: Any) {
        if (item is Doctor) {
            val vh = viewHolder as ViewHolder
            vh.tvTitle.text = item.name
            vh.tvPositionName.text = item.positionName
            vh.tvDepartmentName.text = item.departmentName
            vh.tvConsultationNum.text = "问诊数量：${getConsultationServiceNum(item)}"
            vh.tvGoodAt.text = item.meritIllness
            vh.tvConsultationCommentRate.text = "${getFavorableRate(item)}%"
            vh.imgHead.setUrlRound(item.avatar, dp2px(5f))
            //处方权
            if (item.prescriptionAllow == "1") vh.tvPrescription.visible() else vh.tvPrescription.gone()
            //检验检查权
            if (item.examinationAllow == "1") vh.tvInspect.visible() else vh.tvInspect.gone()
            //住院证权
            if (item.hospitalizedAllow == "1") vh.tvHospitalizationCertificate.visible() else vh.tvHospitalizationCertificate.gone()

            if (itemType == "1") {
                vh.llFollow.visible()
                vh.llConsultationRecord.visible()
            }
        }
    }

    fun getConsultationServiceNum(doctor: Doctor): String {
        doctor.consultationNum?.let {
            return it
        } ?: kotlin.run {
            doctor.consultationServiceNum?.let {
                return it
            }
        }

        return "0"
    }

    private fun getFavorableRate(doctor: Doctor): String {
        doctor.favorableRate?.let {
            return it
        } ?: kotlin.run {
            doctor.consultationCommentRate?.let {
                return it
            }
        }
        return ""
    }

    override fun onUnbindViewHolder(viewHolder: Presenter.ViewHolder) {

    }


    class ViewHolder(view: View) : Presenter.ViewHolder(view) {
        var tvPositionName: TextView = view.findViewById(R.id.tvPositionName)
        var tvTitle: TextView = view.findViewById(R.id.tv_title)
        var tvDepartmentName: TextView = view.findViewById(R.id.tvDepartmentName)
        var tvPrescription: TextView = view.findViewById(R.id.tvPrescription)
        var tvConsultationCommentRate: TextView = view.findViewById(R.id.tvConsultationCommentRate)
        var tvInspect: TextView = view.findViewById(R.id.tvInspect)
        var tvHospitalizationCertificate: TextView =
            view.findViewById(R.id.tvHospitalizationCertificate)
        var tvGoodAt: TextView = view.findViewById(R.id.tvGoodAt)
        var tvConsultationNum: TextView = view.findViewById(R.id.tvConsultationNum)
        var imgHead: ImageView = view.findViewById(R.id.imgHead)
        var itemView: ConstraintLayout = view.findViewById(R.id.itemView)
        var llFollow: ConstraintLayout = view.findViewById(R.id.llFollow)
        var llConsultationRecord: ConstraintLayout = view.findViewById(R.id.llConsultationRecord)
        var layoutParams: ViewGroup.LayoutParams = itemView.layoutParams
    }
}
